//实现一个双向链表
public class MyLinkedList implements ILinkedList{
    //链表中的一个节点
    public class ListNode{
        private int val;
        private ListNode next;
        private ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }
    //双向链表的头和尾
    private ListNode head;
    private ListNode last;

    @Override
    //头插
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        //首先判断是否为空
        if(head == null){
            head = node;
            last = node;
        }else{
            //不为空则正常插入
            node.next = head;
            head.prev = node;
            head = node;
        }

    }

    @Override
    //尾插
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //和头插一样，先判断链表是否为空
        if(head == null){
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
//    求出链表长度
    private int getLenth(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

//    找到下标为index的元素
    private ListNode getNode(int index){
        ListNode cur = head;
        for(int i = 0; i < index; i++){
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public void addIndex(int index, int data) throws IOutOfBoundsException{
        //首先判断index的值是否合法
        if(index < 0 || index > getLenth()){
            throw new IOutOfBoundsException(index);
        }

        //在判断是否是头插和尾插的情况
        //如果链表中只有一个节点,那么只可能是头插或尾插
        if(index == 0){
            addFirst(data);
        }else if(index == getLenth()){
            addLast(data);
        } else {
            ListNode node = new ListNode(data);
            ListNode cur = getNode(index);
            cur.prev.next = node;
            node.next = cur;
            node.prev = cur.prev;
            cur.prev = node;
        }
    }

    @Override
    public boolean contains(int key) {
        //先判断链表是否为空
        if(head == null){
            return false;
        }
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }else{
                cur = cur.next;
            }
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //判断链表是否为空
        if(head == null) {
            return;
        }
        //判断是否只有一个节点
        if(head.val == key && head.next == null){
            head = null;
            last = null;
        }
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //判断头删和尾删
                if(cur == head){
                    head = head.next;
//                    cur = cur.next;
                    head.prev = null;
                    return;
                } else if (cur == last) {
                    last = last.prev;
                    //cur无需修改编译器也会将其指向的节点回收，其生命周期仅限于在改方法中
//                    cur = cur.prev;
                    last.next = null;
                    return;
                }
                //既不是头删也不是尾删
                cur.next.prev = cur.prev;
                cur.prev.next = cur.next;
                return;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        //判断链表是否为空
        if(head == null) {
            return;
        }
        ListNode cur = head.next;
        while (cur != null) {
            //从第二个节点开始判断是否需要删除
            if(cur.val == key){
                //判断是不是尾节点
                if (cur == last) {
                    last = last.prev;
//                cur = cur.prev;
                    last.next = null;
                    break;
                }else {
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
            }
            cur = cur.next;
        }
        //最后判断头节点是否需要删除
        if(head.val == key){
            //如果只剩一个节点
            if(head.next == null){
                head = null;
                last = null;
            }else {
                head = head.next;
                head.prev = null;
            }
        }
    }

    @Override
    public void clear() {
        //两种方法，一种比较暴力，直接将头和尾置为空
//        head = null;
//        last = null;
        //第二种是遍历链表，将每一个节点中的元素都置为空
        if(head == null){
            return;
        }
        ListNode cur = head.next;
        while(cur != null){
            //如果需要也可以将节点中的元素也置为空
//            cur.val = null;
            head.next = null;
            cur.prev = null;
            head = cur;
            cur = cur.next;
        }
        head = null;
        last = null;
    }

    @Override
    //打印出链表中的所有元素
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
